package com.david.learning.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

/**
 * Learn how to use NIO package since JDK 1.4
 * @author David
 *
 */
public class FileChannelTest {
    public static void main(String[] args) {
	File file = new File("E:\\workspaceJava\\learning\\java\\src\\main\\java\\com\\david\\learning\\io\\FileChannelTest.java");
	try {
	    
	    java.nio.channels.FileChannel in = new FileInputStream(file).getChannel();
	    java.nio.channels.FileChannel out =new FileOutputStream("E:\\workspaceJava\\learning\\java\\target\\classes\\com\\david\\learning\\io\\a.txt").getChannel();
	    MappedByteBuffer buffer = in.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
	    out.write(buffer);

	    buffer.clear();
	    Charset charset = Charset.forName("GBK");
	    CharsetDecoder decoder = charset.newDecoder();
	    CharBuffer charBuffer = decoder.decode(buffer);
	    System.out.println(charBuffer);
	    
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }
}
